package com.zhdl.modules.prefiresys.web.service;

import com.zhdl.common.ytools.DateTimeHelper;
import com.zhdl.modules.prefiresys.entity.FactoryLocalHost;
import com.zhdl.modules.prefiresys.service.FactoryLocalHostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * 数据服务
 */
@Slf4j
@Service
public class DatabaseService {

    private final FactoryLocalHostService factoryLocalHostService;

    private final CollectService collectService;

    public DatabaseService(FactoryLocalHostService factoryLocalHostService, CollectService collectService) {

        this.factoryLocalHostService = factoryLocalHostService;
        this.collectService = collectService;
    }

    /**
     * 获取自清洁任务执行频率，单位毫秒
     */
    public long getSelfCleanTaskFrequency() {
        long defval = 24 * 60 * 1000;  // 默认24小时
        List<FactoryLocalHost> listHost = factoryLocalHostService.selectAll();
        if (listHost.size() == 0) return defval;

        FactoryLocalHost factoryLocalHost = listHost.get(0);

        if (factoryLocalHost.getSelfCleanInterval() == null)
            return 24 * 60 * 60 * 1000;

        long interval = factoryLocalHost.getSelfCleanInterval() * 60 * 60 * 1000;
        log.info("自清洁执行频率 {}", DateTimeHelper.msToTimeString(interval));
        return interval;
    }

    /**
     * 获取任务执行延时时间，单位毫秒
     */
    public long getSelfCleanInitialDelay() {
        Integer hour = 1;
        Integer minute = 0;
        List<FactoryLocalHost> listHost = factoryLocalHostService.selectAll();
        if (listHost.size() > 0) {
            FactoryLocalHost factoryLocalHost = listHost.get(0);

            if (factoryLocalHost.getSelfCleanStartHour() != null)
                hour = factoryLocalHost.getSelfCleanStartHour();
            if (factoryLocalHost.getSelfCleanStartMinute() != null)
                minute = factoryLocalHost.getSelfCleanStartMinute();
        }

        // 默认24小时
        long delay = getInitialDelay(hour, minute);
        log.info("自清洁任务延时时间 {}", DateTimeHelper.msToTimeString(delay));
        return delay;
    }

    /**
     * 获取输入时分比当前时间的时间间隔，单位毫秒
     *
     * @param hour
     * @param minute
     * @return
     */
    private long getInitialDelay(Integer hour, Integer minute) {
        LocalDateTime now = LocalDateTime.now();
        LocalTime targetTime = LocalTime.of(hour, minute, 0); // 指定时间为当天 12:00:00
        LocalDateTime nextRun = now.with(targetTime);
        if (now.compareTo(nextRun) >= 0) {
            // 如果当前时间已经超过开始时间，则下一次执行时间为明天同一时间
            nextRun = nextRun.plusDays(1);
        }
        return ChronoUnit.MILLIS.between(now, nextRun); // 计算距离下一次执行任务的时间间隔
    }

    /**
     * 获取数据采集任务执行频率，单位毫秒
     */
    public long getCollectTaskFrequency() {
        long defval = 2 * 1000;  // 默认2秒
        if (collectService.factoryLocalHost == null) {
            List<FactoryLocalHost> listHost = factoryLocalHostService.selectAll();
            if (listHost.size() > 0)
                collectService.factoryLocalHost = listHost.get(0);
        }

        if (collectService.factoryLocalHost == null || collectService.factoryLocalHost.getCollectInterval() == null)
            return defval;

        long interval = collectService.factoryLocalHost.getCollectInterval() * 1000;
        log.info("数据采集任务执行频率 {}", DateTimeHelper.msToTimeString(interval));
        return interval;
    }
}
